package com.ctech.wsgen.writer;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Element;

import com.ctech.wsgen.model.Item;
import com.ctech.wsgen.model.ItemCategory;
import com.ctech.wsgen.model.Site;

public class ItemCategoryDom4JHtmlWriter extends AbstractDom4JHtmlWriter {
	
	private Logger log = Logger.getLogger(ItemCategoryDom4JHtmlWriter.class.getName());
	
	ItemCategory itemCat;
	Site site;
	
	public ItemCategoryDom4JHtmlWriter(ItemCategory itemCategory, Site site) throws Exception {
		
		if(itemCategory != null && itemCategory.getItems() != null) {
			this.itemCat = itemCategory;
			this.site = site;
			File file = new File(site.getSiteDir().getAbsolutePath().concat(File.separator + itemCategory.getFileName()));
			if(!file.exists()) file.createNewFile();
			createHtml(file, this.site.getJavaScriptBlock());		
			if(log.isInfoEnabled()) log.info("Created " + itemCategory.getFileName() + " with " + itemCategory.getItems().size() + " items");
		} else {
			log.error("No items for ItemCategory: " + itemCategory.getCatTitleName());
		}
	}

	@Override
	public String getContentHeaderTitle() {
		return this.itemCat.getCatTitleName();
	}

	@Override
	public List<Item> getItems() {
		return this.itemCat.getItems();
	}

	@Override
	public String getMetaContentName() {
		return this.itemCat.getVendorName();
	}

	@Override
	public String getTitleName() {
		return this.itemCat.getCatTitleName();
	}

	@Override
	public void createContentFooter(Element footerTable) {
		/*
		 *  footer for ItemCategories pages 
		<table>
			<tr>
				<td class="ContentFooter">&nbsp;</td>
			</tr>
		</table>
		*/		
		// Nothing is added for ItemCategory pages		
	}

	@Override
	public String getEmailAddress() {
		return this.site.getStr_email();
	}

	@Override
	public void createContentHeaderTitle(Element divHeader) {
		/*
		<table class="HeaderTitle">
			<tbody>
				<tr>
					<td valign="middle">Electric Strikes</td>
				</tr>
			</tbody>
		</table>
	*/
	divHeader.addElement("table").addAttribute("class", "HeaderTitle")
		.addElement("tbody")
			.addElement("tr")
				.addElement("td").addAttribute("valign", "middle").addText(getContentHeaderTitle());
	
	}

	@Override
	public String getWebsiteURL() {
		return this.site.getStr_websiteUrl();
	}
    
}
